<template>
  <div class="login-container">
    <!-- 左侧品牌区域 -->
    <div class="login-brand">
      <div class="brand-content">
        <h1 class="brand-title">企业管理系统</h1>
        <p class="brand-slogan">高效、安全的企业后台管理解决方案</p>
        <div class="brand-features">
          <div class="feature-item">
            <div class="feature-icon"><Lock /></div>
            <span class="feature-text">安全可靠</span>
          </div>
          <div class="feature-item">
            <div class="feature-icon"><Cpu /></div>
            <span class="feature-text">高效管理</span>
          </div>
          <div class="feature-item">
            <div class="feature-icon"><Setting /></div>
            <span class="feature-text">灵活配置</span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 右侧登录表单 -->
    <div class="login-form-container">
      <el-card class="login-card">
        <div class="card-header">
          <h2 class="login-title">欢迎回来</h2>
          <p class="login-subtitle">请输入您的账号信息</p>
        </div>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="rules"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              class="form-input"
              prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              show-password
              class="form-input"
              prefix-icon="Lock"
            />
          </el-form-item>
          
          <div class="form-options">
            <el-checkbox v-model="rememberMe" class="remember-checkbox">
              记住我
            </el-checkbox>
            <a href="#" class="forgot-password">忘记密码？</a>
          </div>
          
          <el-form-item>
            <el-button
              type="primary"
              @click="handleLogin"
              class="login-button"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="register-link">
          还没有账号？<a href="/register" class="link-text">立即注册</a>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElCard, ElForm, ElFormItem, ElInput, ElButton, ElCheckbox } from 'element-plus'
import { Lock, Cpu, Setting } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import type { LoginRequest } from '../types/user'
import authService from '../services/auth.service'

const router = useRouter()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

// 登录表单数据
const loginForm = ref<LoginRequest>({  
  username: '',
  password: ''
})

// 是否记住我
const rememberMe = ref(false)

// 表单验证规则
const rules = ref<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
  ]
})

// 处理登录
const handleLogin = async () => {
  // 验证表单
  if (!loginFormRef.value) return
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 调用登录接口
    await authService.login(loginForm.value)
    
    ElMessage.success('登录成功')
    router.push('/')
  } catch (error: any) {
    console.error('Login failed:', error)
    ElMessage.error(
      error.response?.data?.message || error.message || '登录失败，请重试'
    )
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
/* 主要颜色变量 */
:root {
  --primary-blue: #3498db;
  --primary-blue-light: #66b1ff;
  --primary-blue-dark: #2980b9;
  --primary-blue-ultralight: #e3f2fd;
  --text-primary: #2c3e50;
  --text-secondary: #7f8c8d;
  --background-light: #f8f9fa;
  --border-radius: 16px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 登录容器 */
.login-container {
  display: flex;
  height: 100vh;
  background: linear-gradient(to bottom, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%);
  overflow: hidden;
  position: relative;
}

/* 大海波浪效果 */
.login-container::before,
.login-container::after {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  top: -50%;
  left: -50%;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 40%;
  animation: wave 15s infinite linear;
}

.login-container::after {
  background: rgba(255, 255, 255, 0.05);
  animation-delay: 7.5s;
}

@keyframes wave {
  0% {
    transform: rotate(0deg) translateX(-25%) translateY(10%);
  }
  100% {
    transform: rotate(360deg) translateX(-25%) translateY(10%);
  }
}

/* 左侧品牌区域 */
.login-brand {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: white;
  position: relative;
  overflow: hidden;
}

/* 品牌背景装饰 */
.login-brand::before {
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  filter: blur(50px);
}

.login-brand::after {
  content: '';
  position: absolute;
  bottom: -50px;
  left: -50px;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  filter: blur(50px);
}

/* 品牌内容 */
.brand-content {
  max-width: 400px;
  z-index: 1;
  animation: fadeInLeft 0.8s ease-out;
}

.brand-title {
  font-size: 3rem;
  font-weight: 700;
  margin: 0 0 20px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.brand-slogan {
  font-size: 1.2rem;
  opacity: 0.9;
  margin: 0 0 40px 0;
  line-height: 1.6;
}

/* 品牌特性 */
.brand-features {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 16px;
  animation: fadeInUp 0.6s ease-out;
}

.feature-item:nth-child(1) {
  animation-delay: 0.2s;
}

.feature-item:nth-child(2) {
  animation-delay: 0.4s;
}

.feature-item:nth-child(3) {
  animation-delay: 0.6s;
}

.feature-icon {
  width: 60px;
  height: 60px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  transition: var(--transition);
}

.feature-item:hover .feature-icon {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.feature-text {
  font-size: 1.1rem;
  font-weight: 500;
}

/* 右侧登录表单容器 */
.login-form-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  position: relative;
}

/* 登录卡片 */
.login-card {
  width: 400px;
  max-width: 90%;
  background: white;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow);
  border: none;
  overflow: hidden;
  animation: fadeInRight 0.8s ease-out;
}

/* 卡片头部 */
.card-header {
  padding: 32px 32px 0 32px;
  text-align: center;
}

.login-title {
  font-size: 2rem;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0 0 8px 0;
}

.login-subtitle {
  font-size: 1rem;
  color: var(--text-secondary);
  margin: 0;
}

/* 登录表单 */
.login-form {
  padding: 32px;
}

/* 表单输入框 */
.form-input {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  transition: var(--transition);
  font-size: 16px;
}

.form-input:hover {
  border-color: var(--primary-blue-light);
  box-shadow: 0 2px 8px rgba(52, 152, 219, 0.1);
}

.form-input:focus {
  border-color: var(--primary-blue);
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.1);
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.remember-checkbox {
  color: var(--text-secondary);
  font-size: 14px;
}

.remember-checkbox :deep(.el-checkbox__label) {
  color: var(--text-secondary);
}

.forgot-password {
  color: var(--primary-blue);
  text-decoration: none;
  font-size: 14px;
  transition: var(--transition);
}

.forgot-password:hover {
  color: var(--primary-blue-dark);
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  background: var(--primary-blue);
  border: none;
  color: white;
  font-size: 16px;
  font-weight: 600;
  transition: var(--transition);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.2);
}

.login-button:hover:not(:disabled) {
  background: var(--primary-blue-dark);
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(52, 152, 219, 0.3);
  color: white;
}

.login-button:disabled {
  opacity: 0.6;
}

/* 注册链接 */
.register-link {
  text-align: center;
  padding: 0 32px 32px 32px;
  font-size: 14px;
  color: var(--text-secondary);
}

.link-text {
  color: var(--primary-blue);
  text-decoration: none;
  font-weight: 500;
  transition: var(--transition);
}

.link-text:hover {
  color: var(--primary-blue-dark);
}

/* 动画 */
@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    height: auto;
    min-height: 100vh;
  }
  
  .login-brand {
    flex: 0;
    padding: 60px 20px 30px 20px;
  }
  
  .brand-title {
    font-size: 2rem;
    text-align: center;
  }
  
  .brand-slogan {
    font-size: 1rem;
    text-align: center;
  }
  
  .brand-features {
    display: none;
  }
  
  .login-form-container {
    flex: 1;
    padding: 40px 20px;
    width: 100%;
  }
  
  .login-card {
    width: 100%;
  }
  
  .card-header {
    padding: 24px 24px 0 24px;
  }
  
  .login-title {
    font-size: 1.6rem;
  }
  
  .login-form {
    padding: 24px;
  }
  
  .register-link {
    padding: 0 24px 24px 24px;
  }
}
</style>